const app = getApp()
var x = 70;
var arrss = []
var arr=[]
var arr1=[]
//中等1   13
var middle1 = [{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 1 + 1,
    id: 0,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/cc.png',
    width: '139',
    height: "139",
    x: x * 1 + 1,
    y: x * 0 + 1,
    id: 1,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: 3 * x + 1,
    y: x * 1 + 1,
    id: 2,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 2 + 1,
    id: 3,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 3 + 1,
    id: 4,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/guanyu.png',
    width: '69',
    height: "139",
    x: x * 1 + 1,
    y: x * 2 + 1,
    id: 5,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 2 + 1,
    y: x * 2 + 1,
    id: 6,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 2 + 1,
    y: x * 3 + 1,
    id: 7,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 2 + 1,
    id: 8,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 3 + 1,
    id: 9,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 4 + 1,
    id: 10,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/zhaoyun.png',
    width: '139',
    height: "69",
    x: x * 1 + 1,
    y: x * 4 + 1,
    id: 11,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 4 + 1,
    id: 12,
  },
]
//中等2   12
var middle2=[
  {
    img: 'https://www.juquduo.com/static/resourse/utils/dao.png',
    width: '69',
    height: "139",
    x: x * 0 + 1,
    y: x * 0 + 1,
    id: 0,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/cc.png',
    width: '139',
    height: "139",
    x: x * 1 + 1,
    y: x * 0 + 1,
    id: 1,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/guanyu.png',
    width: '69',
    height: "139",
    x: x * 3 + 1,
    y: x * 0 + 1,
    id: 2,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/mayan1.png',
    width: '69',
    height: "139",
    x: x * 0 + 1,
    y: x * 2 + 1,
    id: 3,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 1 + 1,
    y: x * 2 + 1,
    id: 4,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 2 + 1,
    y: x * 2 + 1,
    id: 5,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 2 + 1,
    id: 6,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x *1 + 1,
    y: x * 3 + 1,
    id: 7,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x *2 + 1,
    y: x * 3 + 1,
    id: 8,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x *3 + 1,
    y: x * 3 + 1,
    id: 9,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x *0 + 1,
    y: x * 4 + 1,
    id: 10,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 4 + 1,
    id: 11,
  }
]
//简单1   13
var easy1=[{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 0 + 1,
    id: 0,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/cc.png',
    width: '139',
    height: "139",
    x: x * 1 + 1,
    y: x * 0 + 1,
    id: 1,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 0 + 1,
    id: 2,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 1 + 1,
    id: 3,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 1 + 1,
    id: 4,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/zhangnan1.png',
    width: '69',
    height: "139",
    x: x * 0 + 1,
    y: x * 2 + 1,
    id: 5,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 2 + 1,
    id: 6,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 1 + 1,
    y: x * 3 + 1,
    id: 7,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 2 + 1,
    y: x * 3 + 1,
    id: 8,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/guanyu.png',
    width: '69',
    height: "139",
    x: x * 3 + 1,
    y: x * 3 + 1,
    id: 9,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 4 + 1,
    id: 10,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 1 + 1,
    y: x * 4 + 1,
    id: 11,
  },
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 2 + 1,
    y: x * 4 + 1,
    id: 12,
  }
]
//简单2    14
var easy2=[
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 0 + 1,
    id: 0,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 1 + 1,
    y: x * 0 + 1,
    id: 1,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 2 + 1,
    y: x * 0 + 1,
    id: 2,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 0 + 1,
    id: 3,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 1 + 1,
    id: 4,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/cc.png',
    width: '139',
    height: "139",
    x: x * 1 + 1,
    y: x * 1 + 1,
    id: 5,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 1 + 1,
    id: 6,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 2 + 1,
    id: 7,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 2 + 1,
    id: 8,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 3 + 1,
    id: 9,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 1 + 1,
    y: x * 3 + 1,
    id: 10,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 2 + 1,
    y: x * 3 + 1,
    id: 11,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 3 + 1,
    id: 12,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/mayan.png',
    width: '139',
    height: "69",
    x: x * 1 + 1,
    y: x * 4 + 1,
    id: 13,
  },
]
//困难1    10
var diff1=[
  {
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 0 + 1,
    id: 0,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 0 + 1,
    id: 1,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 1 + 1,
    id: 2,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/zhaoyun.png',
    width: '139',
    height: "69",
    x: x * 1 + 1,
    y: x * 1 + 1,
    id: 3,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/dao.png',
    width: '69',
    height: "139",
    x: x * 3 + 1,
    y: x * 1 + 1,
    id: 4,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/zhangnan.png',
    width: '139',
    height: "69",
    x: x * 0 + 1,
    y: x * 2 + 1,
    id: 5,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/guanyu.png',
    width: '69',
    height: "139",
    x: x * 2 + 1,
    y: x * 2 + 1,
    id: 6,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 3 + 1,
    y: x * 3 + 1,
    id: 7,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/cc.png',
    width: '139',
    height: "139",
    x: x * 0 + 1,
    y: x * 3 + 1,
    id: 8,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/mayan.png',
    width: '139',
    height: "69",
    x: x * 2 + 1,
    y: x * 4 + 1,
    id: 9,
  },
]
//困难2    10
var diff2=[
  {
    img: 'https://www.juquduo.com/static/resourse/utils/dao1.png',
    width: '139',
    height: "69",
    x: x * 0 + 1,
    y: x * 0 + 1,
    id: 0,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 2 + 1,
    y: x * 0 + 1,
    id: 1,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/guanyu.png',
    width: '69',
    height: "139",
    x: x * 3 + 1,
    y: x * 0 + 1,
    id: 2,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 1 + 1,
    id: 3,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 0 + 1,
    y: x * 2 + 1,
    id: 4,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/cc.png',
    width: '139',
    height: "139",
    x: x * 1 + 1,
    y: x * 1 + 1,
    id: 5,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/zhangnan1.png',
    width: '69',
    height: "139",
    x: x * 0 + 1,
    y: x * 3 + 1,
    id: 6,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/bing.png',
    width: '69',
    height: "69",
    x: x * 1 + 1,
    y: x * 4 + 1,
    id: 7,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/mayan1.png',
    width: '69',
    height: "139",
    x: x * 2 + 1,
    y: x * 3 + 1,
    id: 8,
  },{
    img: 'https://www.juquduo.com/static/resourse/utils/zhaoyun1.png',
    width: '69',
    height: "139",
    x: x * 3 + 1,
    y: x * 3 + 1,
    id: 9,
  },
]
var num, num1, top, top1, bottom, bottom1, left, left1, right, right1,sitetype,poid;
var top = '1';
var top1 = '1';
var bottom = '1';
var bottom1 = '1';
var left = '1';
var left1 = '1';
var right = '1';
var right1 = '1';
Page({
  data: {
    windowWidth: app.windowWidth, // 屏幕宽度
    countDownMinute: 4,
    countDownSecond: 59,
    arr: [],
    lastX: 0,
    lastY: 0,
    text: '',
    arr1: [],
    pid:0,
    num:num,
    nandu:0
  },
  //触摸滑动事件
  touchStart: function (event) {
    console.log(event)
    var that = this
    for (var i = 0; i < that.data.arr.length; i++) {
      if (event.target.id == i) {
        var width1 = Math.round(arr[i].width / 70); //1
        var height1 = Math.round(arr[i].height / 70); //1
        var x1 = Math.round(arr[i].x / 70); //0
        var y1 = Math.round(arr[i].y / 70); //1
        var xx1 = x1 * 5 + y1 //20个数组的下标
        // console.log(xx1)
        // console.log(arrss[xx1 - 5])
        if (arr[i].width == 69 && arr[i].height == 69) { //完成
          num = 211
          num1 = 281
          top = arrss[xx1 - 1]
          top1 = '1'
          bottom = arrss[xx1 + 1]
          bottom1 = '1'
          left = arrss[xx1 - 5]
          left1 = '1'
          right1 = '1'
          right = arrss[xx1 + 5]
        }
        if (arr[i].width == 69 && arr[i].height == 139) { //完成
          num = 211
          num1 = 211
          top = arrss[xx1 - 1]
          top1 = '1'
          bottom = arrss[xx1 + 2]
          bottom1 = '1'
          left = arrss[xx1 - 5]
          left1 = arrss[xx1 - 4]
          right = arrss[xx1 + 5]
          right1 = arrss[xx1 + 6]
        }
        if (arr[i].width == 139 && arr[i].height == 139) { //完成
          num = 141
          num1 = 211
          top = arrss[xx1 - 1]
          top1 = arrss[xx1 + 4]
          bottom = arrss[xx1 + 2]
          bottom1 = arrss[xx1 + 7]
          left = arrss[xx1 - 5]
          left1 = arrss[xx1 - 4]
          right = arrss[xx1 + 10]
          right1 = arrss[xx1 + 11]

        }
        if (arr[i].width == 139 && arr[i].height == 69) { //完成
          num = 141
          num1 = 281
          top = arrss[xx1 - 1]
          top1 = arrss[xx1 + 4]
          bottom = arrss[xx1 + 1]
          bottom1 = arrss[xx1 + 6]
          left = arrss[xx1 - 5]
          left1 = '1'
          right1 = '1'
          right = arrss[xx1 + 10]
        }
      }
    }
    var that = this;
    that.data.lastX = event.changedTouches[0].pageX
    that.data.lastY = event.changedTouches[0].pageY
  },
  touchEnd(event) {
    console.log(event)
    var that = this;
    let currentX = event.changedTouches[0].pageX
    let currentY = event.changedTouches[0].pageY
    let tx = currentX - this.data.lastX
    let ty = currentY - this.data.lastY
    let text = "";
    for (var i = 0; i < that.data.arr.length; i++) {
      if (event.target.id == i) { //1
        if (Math.abs(tx) > Math.abs(ty)) {
          //左右方向滑动
          if (tx < 0 && (arr[i].x != 1 || arr[i].x > num) && (left == '1' && left1 == '1')) {
            text = "向左滑动"
            arr[i].x = arr[i].x - 70
          } else if (tx > 0 && arr[i].x < num && (right == '1' && right1 == '1')) {
            text = "向右滑动"
            arr[i].x = arr[i].x + 70
          }
        } else {
          //上下方向滑动
          if (ty < 0 && (arr[i].y != 1 || arr[i].y > num1) && (top == '1' && top1 == '1')) {
            text = "向上滑动"
            arr[i].y = arr[i].y - 70

          } else if (ty > 0 && arr[i].y < num1 && (bottom == '1' && bottom1 == '1')) {

            text = "向下滑动"
            arr[i].y = arr[i].y + 70
          }
        }
      }
      var chu=event.currentTarget.dataset.aa
      //如果宽高都是139 说明是正在移动曹操的方块。当方块移动到底部跳转页面
      if ((chu.width=='139')&&(chu.height=='139')) {
        console.log(chu.id)
        var j=chu.id
        if (arr[j].y >= 211 && arr[j].x == 71) {
          console.log(poid,sitetype)
          wx.redirectTo({
             url: '../dgameques/dgameques?state=1&pid='+poid+'&sitetype='+sitetype,
          })
        }
      }
    }
    //将当前坐标进行保存以进行下一次计算
    that.data.lastX = currentX
    that.data.lastY = currentY

    this.getstate()
    that.setData({
      text: text,
      arr: arr
    });
  },
  getstate() {
    arrss = []
    for (var i = 0; i < 20; i++) { //让20个默认都是1  1代表为空格
      arrss[i] = '1'
    }
    for (var i = 0; i < arr.length; i++) {
      //比如小兵位置是x=0  y=140  下标就是0*5+2 =2  相当于竖着排列第三个小兵
      var width = Math.round(arr[i].width / 70); //1
      var height = Math.round(arr[i].height / 70); //1
      var x = Math.round(arr[i].x / 70); //0
      var y = Math.round(arr[i].y / 70); //1
      var xx = x * 5 + y //1
      // console.log(xx,width,height,x,y)
      if (width == '1' && height == '1') { //小兵所占的方格
        arrss[xx] = '2'
      } else if (width == '1' && height == '2') { //晏明所占的方格
        arrss[xx] = '2';
        arrss[xx + 1] = '2'
      } else if (width == '2' && height == '1') { //张赫所占的方格
        arrss[xx] = '2';
        arrss[xx + 5] = '2'
      } else if (width == '2' && height == '2') { //赵云所占的方格
        arrss[xx] = '2';
        arrss[xx + 1] = '2'
        arrss[xx + 6] = '2';
        arrss[xx + 5] = '2'
      }
    }
    // console.log(arrss)
  },
  //重置键
  chongzhi(){
    var that=this;
    // console.log(that.data.num)
    // console.log(that.data.arr)
    console.log(easy1,arr)
    if(that.data.num==1){
      if(that.data.nandu=='1'){
        arr=JSON.parse(JSON.stringify(easy1))
        that.setData({
          arr:arr
        })
      }else if(that.data.nandu=='2'){
        arr=JSON.parse(JSON.stringify(middle1))
        that.setData({
          arr:arr
        })
      }else if(that.data.nandu=='3'){
        arr=JSON.parse(JSON.stringify(diff1))
        that.setData({
          arr:arr
        })
      }
    }else if(that.data.num==2){
      if(that.data.nandu=='1'){
       arr=JSON.parse(JSON.stringify(easy2))
       that.setData({
        arr:arr
      })
          
      }else if(that.data.nandu=='2'){
        arr=JSON.parse(JSON.stringify(middle2))
        that.setData({
          arr:arr
        })
         
      }else if(that.data.nandu=='3'){
        arr=JSON.parse(JSON.stringify(diff2))
        that.setData({
          arr:arr
        })
      }
    }
  },
  onLoad: function (options) {
    poid = options.pid
    sitetype = options.sitetype
    console.log(options)
    var that = this;
    that.data.pid=options.pid
    that.data.nandu=options.nandu
    var num=Math.floor(Math.random() * (3 - 1)) + 1
    that.data.num=num
    // console.log(options.nandu)

    if(options.pid=='5'){
      num==1
      console.log(num)
    }else if(options.pid=='8'){
      num==2
      console.log(num)
    }
    //根据难度判断出来哪一个棋盘
    if(num==1){
      if(options.nandu=='1'){
        arr=JSON.parse(JSON.stringify(easy1))
        that.setData({
          arr:arr
        })
      }else if(options.nandu=='2'){
        arr=JSON.parse(JSON.stringify(middle1))
        that.setData({
          arr:arr
        })
          
      }else if(options.nandu=='3'){
        arr=JSON.parse(JSON.stringify(diff1))
        that.setData({
          arr:arr
        })
         
      }
    }else if(num==2){
      if(options.nandu=='1'){
        arr=JSON.parse(JSON.stringify(easy1))
        that.setData({
          arr:arr
        })
          
      }else if(options.nandu=='2'){
        arr=JSON.parse(JSON.stringify(middle2))
        that.setData({
          arr:arr
        })
         
      }else if(options.nandu=='3'){
        arr=JSON.parse(JSON.stringify(diff2))
        that.setData({
          arr:arr
        })
          
      }
    }
    //计时器
    var timer = setInterval(function () {
      var minute = that.data.countDownMinute
      var second = that.data.countDownSecond
      second--
      if (second == -1) {
        minute = that.data.countDownMinute - 1
        second = 59
      }
      if(second<10){
        second='0'+second
      }
      // console.log(second)
      that.setData({
        countDownSecond: second,
        countDownMinute: minute
      })
      if (minute == 0 && second == 0) {
        clearInterval(timer)
        wx.navigateTo({
          url: './log',
        })
      }
    }, 1000)
  },
})